package com.zang.blogz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zang.blogz.annotation.OptLog;
import com.zang.blogz.constant.OptTypeConstant;
import com.zang.blogz.entity.Resource;
import com.zang.blogz.model.output.ResourceVO;
import com.zang.blogz.result.Result;
import com.zang.blogz.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朵橙i
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/blogz/resource")
@Api(tags = "权限模块")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("/admin/queryResourceByRoleId")
    @ApiOperation(value = "根据角色id得到其资源")
    public Result<List<Resource>> queryResourceIdByRoleId(Integer roleId) {

        return Result.ok(resourceService.queryResourceByRoleId(roleId));
    }

    @GetMapping("/resourceTree")
    @ApiOperation(value = "得到资源树")
    public Result<List<ResourceVO>> getResourceTreeBy() {

        return Result.ok(resourceService.getResourceTree());
    }

    @PostMapping("/save")
    @ApiOperation(value = "添加资源", notes = "添加资源")
    @OptLog(optType = OptTypeConstant.SAVE)
    public Result<Boolean> saveResource(@RequestBody Resource resource){

        return Result.ok(resourceService.save(resource));
    }

    @GetMapping("/queryResourceByParentId")
    @ApiOperation(value = "获取当前资源下有多少子资源", notes = "获取当前资源下有多少子资源")
    public Result<List<Resource>> queryResourceByParentId(Integer parentId){

        return Result.ok(resourceService.list(new LambdaQueryWrapper<Resource>().eq(Resource::getParentId, parentId)));
    }




}
